'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/template/kh/getSuncode 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 500, msg: "生成小程序码失败" };
		// 业务逻辑开始-----------------------------------------------------------
		
		const { page, env_version, scene } = data;
		let getUnlimitedRes = await vk.openapi.weixin.wxacode.getUnlimited({
		  page,
		  scene,
		  check_path: false,
		  env_version, // 要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"。默认是正式版。
		});
		if (typeof getUnlimitedRes === "object" && getUnlimitedRes.code) {
		  return getUnlimitedRes;
		}
		try {
		  // 二进制转base64
		  let base64 = Buffer.from(getUnlimitedRes, 'binary').toString('base64')
		  return {
		    code: 0,
		    base64: `data:image/png;base64,${base64}`
		  };
		} catch (err) {
		  // 转base64失败
		  return {
		    code: -1,
		    msg: "生成小程序码失败",
		    err: {
		      message: err.message,
		      stack: err.stack
		    }
		  };
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
